
#include "stdafx.h"
#include "pf_selection.h"

#include "pf_information.h"
#include "pf_polyline.h"
#include "pf_entity.h"
#include "pf_graphic.h"
#include "pf_layer.h"



/**
 * Default constructor.
 *
 * @param container The container to which we will add
 *        entities. Usually that's an PF_Graphic entity but
 *        it can also be a polyline, text, ...
 */
PF_Selection::PF_Selection(PF_Container& container,
                           PF_GraphicView* graphicView) {
    this->container = &container;
    this->graphicView = graphicView;
    graphic = container.getGraphic();
}



/**
 * Selects or deselects the given entitiy.
 */
void PF_Selection::selectSingle(PF_Entity* e) {
    if (e!=NULL && (e->getLayer()==NULL || e->getLayer()->isLocked()==false)) {

        if (graphicView!=NULL) {
            graphicView->deleteEntity(e);
        }

       	e->toggleSelected();

        if (graphicView!=NULL) {
            graphicView->drawEntity(e);
        }
    }
}



/**
 * Selects all entities on visible layers.
 */
void PF_Selection::selectAll(bool select) {
    if (graphicView!=NULL) {
        //graphicView->deleteEntity(container);
    }

    //container->setSelected(select);
    for (PF_Entity* e=container->firstEntity();
             e!=NULL;
             e=container->nextEntity()) {
    //for (uint i=0; i<container->count(); ++i) {
        //PF_Entity* e = container->entityAt(i);

        if (e!=NULL && e->isVisible()) {
            e->setSelected(select);
        }
    }

    if (graphicView!=NULL) {
        //graphicView->drawEntity(container);
		graphicView->redraw();
    }
}



/**
 * Selects all entities on visible layers.
 */
void PF_Selection::invertSelection() {
    if (graphicView!=NULL) {
        //graphicView->deleteEntity(container);
    }

    for (PF_Entity* e=container->firstEntity(); e!=NULL;
            e=container->nextEntity()) {
    //for (uint i=0; i<container->count(); ++i) {
        //PF_Entity* e = container->entityAt(i);

        if (e!=NULL && e->isVisible()) {
            e->toggleSelected();
        }
    }

    if (graphicView!=NULL) {
        //graphicView->drawEntity(container);
		graphicView->redraw();
    }
}



/**
 * Selects all entities that are completely in the given window.
 *
 * @param v1 First corner of the window to select.
 * @param v2 Second corner of the window to select.
 * @param select true: select, false: deselect
 */
void PF_Selection::selectWindow(const PF_Vector& v1, const PF_Vector& v2,
                                bool select, bool cross) {

    container->selectWindow(v1, v2, select, cross);

    if (graphicView!=NULL) {
		graphicView->redraw();
    }
}



/**
 * Selects all entities that are intersected by the given line.
 *
 * @param v1 Startpoint of line.
 * @param v2 Endpoint of line.
 * @param select true: select, false: deselect
 */
void PF_Selection::selectIntersected(const PF_Vector& v1, const PF_Vector& v2,
                                     bool select) {

    PF_Line line(NULL, PF_LineAttribute(v1, v2));
    bool inters;

    for (PF_Entity* e=container->firstEntity(); e!=NULL;
            e=container->nextEntity()) {
    //for (uint i=0; i<container->count(); ++i) {
        //PF_Entity* e = container->entityAt(i);

        if (e!=NULL && e->isVisible()) {

            inters = false;

            // select containers / groups:
            if (e->isContainer()) {
                PF_Container* ec = (PF_Container*)e;

                for (PF_Entity* e2=ec->firstEntity(LX_Define::ResolveAll); e2!=NULL;
                        e2=ec->nextEntity(LX_Define::ResolveAll)) {

                    PF_VectorSolutions sol =
                        PF_Information::getIntersection(&line, e2, true);

                    if (sol.hasValid()) {
                        inters = true;
                    }
                }
            } else {

                PF_VectorSolutions sol =
                    PF_Information::getIntersection(&line, e, true);

                if (sol.hasValid()) {
                    inters = true;
                }
            }

            if (inters) {
                if (graphicView!=NULL) {
                    graphicView->deleteEntity(e);
                }

                e->setSelected(select);

                if (graphicView!=NULL) {
                    graphicView->drawEntity(e);
                }
            }
        }
    }

}



/**
 * Selects all entities that are connected to the given entity.
 *
 * @param e The entity where the algorithm starts. Must be an atomic entity.
 */
void PF_Selection::selectContour(PF_Entity* e) {

    if (e==NULL) {
        return;
    }

    if (!e->isAtomic()) {
        return;
    }

    bool select = !e->isSelected();
    PF_AtomicEntity* ae = (PF_AtomicEntity*)e;
    PF_Vector p1 = ae->getStartpoint();
    PF_Vector p2 = ae->getEndpoint();
    bool found = false;

    // (de)select 1st entity:
    if (graphicView!=NULL) {
        graphicView->deleteEntity(e);
    }
    e->setSelected(select);
    if (graphicView!=NULL) {
        graphicView->drawEntity(e);
    }

    do {
        found = false;

        for (PF_Entity* en=container->firstEntity(); en!=NULL;
                en=container->nextEntity()) {
        //for (uint i=0; i<container->count(); ++i) {
            //PF_Entity* en = container->entityAt(i);

            if (en!=NULL && en->isVisible() && 
				en->isAtomic() && en->isSelected()!=select && 
				(en->getLayer()==NULL || en->getLayer()->isLocked()==false)) {

                ae = (PF_AtomicEntity*)en;
                bool doit = false;

                // startpoint connects to 1st point
                if (ae->getStartpoint().distanceTo(p1)<1.0e-4) {
                    doit = true;
                    p1 = ae->getEndpoint();
                }

                // endpoint connects to 1st point
                else if (ae->getEndpoint().distanceTo(p1)<1.0e-4) {
                    doit = true;
                    p1 = ae->getStartpoint();
                }

                // startpoint connects to 2nd point
                else if (ae->getStartpoint().distanceTo(p2)<1.0e-4) {
                    doit = true;
                    p2 = ae->getEndpoint();
                }

                // endpoint connects to 1st point
                else if (ae->getEndpoint().distanceTo(p2)<1.0e-4) {
                    doit = true;
                    p2 = ae->getStartpoint();
                }

                if (doit) {
                    if (graphicView!=NULL) {
                        graphicView->deleteEntity(ae);
                    }
                    ae->setSelected(select);
                    if (graphicView!=NULL) {
                        graphicView->drawEntity(ae);
                    }
                    found = true;
                }
            }
        }
    } while(found);
}



/**
 * Selects all entities on the given layer.
 */
void PF_Selection::selectLayer(PF_Entity* e) {

    if (e==NULL) {
        return;
    }

    bool select = !e->isSelected();

    PF_Layer* layer = e->getLayer(true);
    if (layer==NULL) {
        return;
    }

    QString layerName = layer->getName();
	selectLayer(layerName, select);
}



/**
 * Selects all entities on the given layer.
 */
void PF_Selection::selectLayer(const QString& layerName, bool select) {

    for (PF_Entity* en=container->firstEntity(); en!=NULL;
            en=container->nextEntity()) {

        if (en!=NULL && en->isVisible() && 
				en->isSelected()!=select && 
				(en->getLayer()==NULL || en->getLayer()->isLocked()==false)) {

            PF_Layer* l = en->getLayer(true);

            if (l!=NULL && l->getName()==layerName) {
                if (graphicView!=NULL) {
                    graphicView->deleteEntity(en);
                }
                en->setSelected(select);
                if (graphicView!=NULL) {
                    graphicView->drawEntity(en);
                }
            }
        }
    }
}

// EOF
